package com.my.attendance.controller;

import com.my.attendance.entity.Department;
import com.my.attendance.entity.Employee;
import com.my.attendance.entity.Position;
import com.my.attendance.service.DepartmentService;
import com.my.attendance.service.EmployeeService;
import com.my.attendance.service.PositionService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/EmployeeDetailServlet")
public class EmployeeDetailServlet extends HttpServlet {
    private DepartmentService departmentService=new DepartmentService();
    private PositionService positionService=new PositionService();
    private EmployeeService employeeService=new EmployeeService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            String id = req.getParameter("id");
            Employee employee = employeeService.getEmployee(Integer.parseInt(id));
            Integer department_number = employee.getDepartment_number();
            Integer position_number = employee.getPosition_number();
            Department department = departmentService.getDepartment(department_number);
            Position position = positionService.getPosition(position_number);
            employee.setDepartment(department);
            employee.setPosition(position);
            req.setAttribute("employee",employee);
            req.getRequestDispatcher("jsp/admin/employee_detail.jsp").forward(req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
